package com.wt.struggle.goodbye.user.adapter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.FileInfo
import com.wt.struggle.goodbye.info.PhotoInfo
import com.wt.struggle.goodbye.main.adapter.PicAdapter
import com.wt.struggle.goodbye.user.activity.ShowImageActivity
import com.xin.lv.yang.utils.adapter.AdapterCallBack
import com.xin.lv.yang.utils.utils.DataUtil
import kotlinx.android.synthetic.main.my_photo_item.view.*

class PhotoAdapter(context: Context, list: ArrayList<PhotoInfo>) : ProBaseAdapter<PhotoInfo>(context, list) {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val photoInfo = list[position]
        val vh = holder as VH

        showSonAdapter(vh, photoInfo)

    }

    private fun showSonAdapter(vh: PhotoAdapter.VH, photoInfo: PhotoInfo) {
        val sonList = photoInfo.imagesInfos

        if (sonList!!.size == 0) {
            vh.tvSonTime.visibility = View.GONE
        } else {

            vh.tvSonTime.visibility = View.VISIBLE
            val picInfo = sonList[0]

            vh.tvSonTime.text = DataUtil.longToTime(picInfo.createtime / 1000, "yyyy年MM月dd日")
        }

        val picList = arrayListOf<FileInfo>()

        for (temp in sonList) {
            val file = FileInfo()

            val IP = temp.domainname

            file.text = if (IP != null && IP != "" && IP != "null") {
                IP + temp.pathThumb.subSequence(1, temp.pathThumb.length)
            } else {
                Config.GET_IMAGE_URL + temp.id
            }

            picList.add(file)
        }

        val len = picList.size

        vh.flexRecyclerView.isNestedScrollingEnabled = false
        vh.flexRecyclerView.setHasFixedSize(true)

        val manager = object : GridLayoutManager(context, 3) {

            override fun canScrollVertically(): Boolean {
                return false
            }
        }

        vh.flexRecyclerView.layoutManager = manager

        val w = (getW(context) - context.resources.getDimension(R.dimen.dp_20).toInt()) / 3

        val num1 = len / 3
        val num2 = len % 3

        val h = if (num1 == 0) {
            if (num2 != 0) {
                w
            } else {
                0
            }
        } else {
            if (num2 == 0) {
                w * num1
            } else {
                w * (num1 + 1)
            }
        }

        vh.flexRecyclerView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, h)

        val adapter = PicAdapter(context, picList)
        adapter.code = 4
        vh.flexRecyclerView.adapter = adapter

        adapter.itemClickListener = object : ItemClickListener {

            override fun onItemClick(position: Int) {

                val picArr = arrayListOf<String>()

                for (tt in sonList) {
                    picArr.add(tt.domainname + tt.path.subSequence(1, tt.path.length))
                }

                val intent = Intent(context, ShowImageActivity::class.java)
                intent.putStringArrayListExtra("arr", picArr)
                context.startActivity(intent)
            }

            override fun onLongClick(position: Int) {

            }

        }


    }

    val newList = arrayListOf<PhotoInfo>()

    fun change(bb: Boolean) {
        newList.clear()
        newList.addAll(list)

        for (temp in newList) {
            temp.isShow = bb
        }

        update(newList, object : AdapterCallBack<PhotoInfo>(list, newList) {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return true;
            }

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return false
            }

            override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
                val bundle = Bundle()
                val oInfo = oldList[oldItemPosition]
                val nInfo = newList[newItemPosition]
                bundle.putParcelable("info", nInfo)
                return bundle
            }

        })

    }


    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return VH(inflater.inflate(R.layout.my_photo_item, parent, false))
    }


    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {


    }


    inner class VH(view: View) : RecyclerView.ViewHolder(view) {
        val flexRecyclerView = view.flexRecyclerView
        val tvSonTime = view.tvSonTime


    }
}